/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp.xmlrpc.library;

import dk.redia.lib.xmlrpc.XmlRpcClientCallTask;
import dk.redia.lib.xmlrpc.XmlRpcClientResultListener;


public class XmlRpcClient {
	
	private static XmlRpcClient instance;
	
	public static XmlRpcClient getInstance() {
		if (instance==null) {
			instance = new XmlRpcClient();
		}
		return instance;
	}
	
	public XmlRpcClient() {
	}
	
	/**
	 * Search for shit.
	 * @param string $query The search query
	 * @param int $maxItems The max number of items (max 20)
	 * @param int $offSet show from which search result
	 * @return mixed a Standard FailureArray if something goes wrong, otherwise a Standard SuccessArray with a array of search results
	 */
	public XmlRpcClientCallTask search(CharSequence customerId, CharSequence apiKey, String query, int maxItems, int offset, String documentType, XmlRpcClientResultListener l) {
		LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
		c.resultListener = l;
		c.methodName = "search";
		c.params = new Object[] { 
				customerId.toString(), 
				apiKey.toString(), 
				query, 
				maxItems, 
				offset, 
				documentType
		};
		XmlRpcClientCallTask task = new XmlRpcClientCallTask();
		task.executeManaged(c);
//		AsyncTaskManager.getInstance().taskReady(task);
//		task.executeManaged(c);
    	return task;
	}

	public XmlRpcClientCallTask getObjectExternals(CharSequence customerId, CharSequence apiKey, String[] faustIds, String[] externalsKeys, XmlRpcClientResultListener l) {
		LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
		c.resultListener = l;
		c.methodName = "getObjectExternals";
		c.params = new Object[] { customerId.toString(), apiKey.toString(), faustIds, externalsKeys };
		XmlRpcClientCallTask task = new XmlRpcClientCallTask();
		task.executeManaged(c);
		return task;
	}
	
	public XmlRpcClientCallTask getObjectHasExternals(CharSequence customerId, CharSequence apiKey, String[] faustIds, String[] externalsKeys, XmlRpcClientResultListener l) {
		LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
		c.resultListener = l;
		c.methodName = "getObjectExternals";
		c.params = new Object[] { customerId.toString(), apiKey.toString(), faustIds, externalsKeys };
		XmlRpcClientCallTask task = new XmlRpcClientCallTask();
		task.executeManaged(c);
		return task;
	}
	
	public XmlRpcClientCallTask getObjectIdByBarcodeId(CharSequence customerId, CharSequence apiKey, String[] barcodeIds, XmlRpcClientResultListener l) {
		LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
		c.resultListener = l;
		c.methodName = "getObjectIdByBarcodeId";
		c.params = new Object[] { customerId.toString(), apiKey.toString(), barcodeIds };
		XmlRpcClientCallTask task = new XmlRpcClientCallTask();
		task.executeManaged(c);
		return task;
	}
	
    public XmlRpcClientCallTask getReservationBranches(CharSequence customerId, CharSequence apiKey, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getReservationBranches";
    	c.params = new Object[] { customerId.toString(), apiKey.toString() };
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    
    public XmlRpcClientCallTask getPatronInformationExtended(CharSequence customerId, CharSequence apiKey, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getPatronInformationExtended";
    	c.params = new Object[] { customerId.toString(), apiKey.toString() };
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    
    public XmlRpcClientCallTask addReservation(CharSequence customerId, CharSequence apiKey, String resourceId, String pickupBranch, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "addReservation";
    	c.params = new Object[] { 
    			customerId.toString(), 
    			apiKey.toString(),
    			resourceId, 
    			pickupBranch 
		};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    /* Temporarily disabled for performance reasons.
    public XmlRpcClientCallTask removeReservation(CharSequence customerId, CharSequence apiKey, String reservationId, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "removeReservation";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString(),
    			reservationId
    	};
    	new XmlRpcClientCallTask().execute(c);
    }
    */
    public XmlRpcClientCallTask removeReservations(CharSequence customerId, CharSequence apiKey, String[] reservationIds, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "removeReservations";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString(),
    			reservationIds
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    
    public XmlRpcClientCallTask renewAllLoans(CharSequence customerId, CharSequence apiKey, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "renewAllLoans";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString()
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    public XmlRpcClientCallTask renewLoan(CharSequence customerId, CharSequence apiKey, String[] loanIds, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "renewLoan";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString(),
    			loanIds
		};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    
    public XmlRpcClientCallTask getReservations(CharSequence customerId, CharSequence apiKey, int maxItems, int offSet, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getReservations";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString(),
    			maxItems,
    			offSet
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    public XmlRpcClientCallTask getLoans(CharSequence customerId, CharSequence apiKey, int maxItems, int offSet, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getLoans";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString(),
    			maxItems,
    			offSet
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    
    public XmlRpcClientCallTask deauthenticate(CharSequence customerId, CharSequence apiKey, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "authenticate";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString()
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    public XmlRpcClientCallTask authenticate(CharSequence customerId, CharSequence apiKey, String username, String password, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "authenticate";
    	c.params = new Object[] { 
    			customerId.toString(),
    			apiKey.toString(),
    			username,
    			password
		};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }

    public XmlRpcClientCallTask getOverdueLoans(CharSequence customerId, CharSequence apiKey, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getOverdueLoans";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString()
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }

    public XmlRpcClientCallTask getReadyReservations(CharSequence customerId, CharSequence apiKey, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getReadyReservations";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString()
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    
    public XmlRpcClientCallTask getCoverUrl(CharSequence customerId, CharSequence apiKey, String[] ids, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getCoverUrl";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString(),
    			ids
		};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }

    public XmlRpcClientCallTask getOpeningHoursData(CharSequence customerId, CharSequence apiKey, String bib, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getOpeningHoursData";
    	c.params = new Object[] {
    			customerId,
    			apiKey.toString(),
    			bib
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }

    public XmlRpcClientCallTask getOpeningHours(CharSequence customerId, CharSequence apiKey, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getOpeningHours";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString()
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    
    public XmlRpcClientCallTask getLibraryListData(CharSequence customerId, CharSequence apiKey, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getLibraryListData";
    	c.params = new Object[] {
    			customerId.toString(),
    			apiKey.toString()
    	};
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    
    public XmlRpcClientCallTask getObject(CharSequence customerId, CharSequence apiKey, String id, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getObject";
    	c.params = new Object[] { customerId.toString(), apiKey.toString(), id };
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
    
    public XmlRpcClientCallTask getObjectExtras(CharSequence customerId, CharSequence apiKey, String[] ids, XmlRpcClientResultListener l) {
    	LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
    	c.resultListener = l;
    	c.methodName = "getObjectExtras";
    	c.params = new Object[] { customerId.toString(), apiKey.toString(), ids };
    	XmlRpcClientCallTask task = new XmlRpcClientCallTask();
    	task.executeManaged(c);
    	return task;
    }
	
	public XmlRpcClientCallTask isSupported(CharSequence customerId, CharSequence apiKey, XmlRpcClientResultListener l) {
		LibraryXmlRpcClientCall c = new LibraryXmlRpcClientCall();
		c.resultListener = l;
		c.methodName = "isSupported";
		c.params = new Object[] {
				customerId.toString(),
				apiKey.toString()
		};
		XmlRpcClientCallTask task = new XmlRpcClientCallTask();
		task.executeManaged(c);
//		AsyncTaskManager.getInstance().taskReady(task);
		return task;
	}
}
